using System;
using System.Data;
using System.Windows.Forms;
using TUnit.Data;
using TUnit.Class;

namespace viclip
{
    ///<summary>
    ///Date init:  
    ///Date modify:
    ///By:
    ///
    ///</summary>
    public class CNACategories : Class
    {
        #region Declares
        int _CategoriesId = -1;
        string _CategoriesName = "";
        #endregion

        #region Constructors
        public CNACategories()
        {
        }
        public CNACategories(CLogin Login)
        {
            base.Login = Login;
        }
        public CNACategories(CLogin Login, object ObjId)
        {
            base.Login = Login;
            using (AccDBNACategories db = new AccDBNACategories())
            {
                this.Populate((CNACategories)db.GetClass(Login, ObjId));
            }
        }
        public CNACategories(CLogin Login, IProvider p, System.Data.DataRow dr)
        {
            base.Login = Login;
            this.Populate(p, dr);
        }
        public CNACategories(CLogin Login, IProvider p, object ObjId)
        {
            base.Login = Login;
            using (AccDBNACategories db = new AccDBNACategories())
            {
                this.Populate((CNACategories)db.GetClass(Login, p, ObjId));
            }
        }
        #endregion

        #region Methods
        public void Populate(IProvider p, DataRow dr)
        {
            if (dr != null)
            {
                try
                {
                    if (dr.Table.Columns.IndexOf("CategoriesId") >= 0)
                    {
                        this._CategoriesId = (dr["CategoriesId"] != DBNull.Value ? Convert.ToInt32(dr["CategoriesId"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblNACategories ADD CategoriesId [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("CategoriesName") >= 0)
                    {
                        this._CategoriesName = (dr["CategoriesName"] != DBNull.Value ? Convert.ToString(dr["CategoriesName"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblNACategories ADD CategoriesName [nvarchar] (50) NULL");
                    //}
                }
                catch { }
            }
        }
        public void Populate(CNACategories Obj)
        {
            if (Obj != null)
            {
                try
                {
                    this._CategoriesId = Obj.CategoriesId;
                    this._CategoriesName = Obj.CategoriesName;
                }
                catch { }
            }
        }
        public override bool Save()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBNACategories db = new AccDBNACategories())
                    {
                        if (this.EventSaving != null)
                            this.EventSaving(this);
                        if (db.Save(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaved != null)
                                this.EventSaved(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaveError != null)
                                this.EventSaveError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Save(IProvider p)
        {
            using (AccDBNACategories db = new AccDBNACategories())
            {
                if (db.Save(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override bool Delete()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBNACategories db = new AccDBNACategories())
                    {
                        if (this.EventDeleting != null)
                            this.EventDeleting(this);
                        if (db.Delete(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleted != null)
                                this.EventDeleted(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleteError != null)
                                this.EventDeleteError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Delete(IProvider p)
        {
            using (AccDBNACategories db = new AccDBNACategories())
            {
                if (db.Delete(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        #endregion

        #region Events
        public delegate void OnEventSaveError(CNACategories obj);
        public event OnEventSaveError EventSaveError;
        public delegate void OnEventSaving(CNACategories CategoriesVideos);
        public event OnEventSaving EventSaving;
        public delegate void OnEventSaved(CNACategories CategoriesVideos);
        public event OnEventSaved EventSaved;
        public delegate void OnEventDeleting(CNACategories CategoriesVideos);
        public event OnEventSaving EventDeleting;
        public delegate void OnEventDeleted(CNACategories CategoriesVideos);
        public event OnEventDeleted EventDeleted;
        public delegate void OnEventDeleteError(CNACategories obj);
        public event OnEventDeleteError EventDeleteError;
        #endregion

        #region Properties
        public int CategoriesId
        {
            get { return this._CategoriesId; }
            set
            {
                if (this._CategoriesId != value)
                {
                    this._CategoriesId = value;
                }
            }
        }
        public string CategoriesName
        {
            get { return this._CategoriesName; }
            set
            {
                if (this._CategoriesName != value)
                {
                    this._CategoriesName = value;
                }
            }
        }

        /// <summary>
        /// Hien ten menu top cua chuyen muc
        /// </summary>
        public string DisplayMenuName
        {
            get {
                if (this.MenuItemCollection != null && this.MenuItemCollection.Count > 0)
                    return ((CNAMenuItem)this.MenuItemCollection[0]).menuitemname;
                return "..."; 
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public int MenuItemIdTop
        {
            get
            {
                if (this.MenuItemCollection != null && this.MenuItemCollection.Count > 0)
                    return ((CNAMenuItem)this.MenuItemCollection[0]).menuitemid;
                return -1;
            }
        }

        CCollection _MenuItemCollection = null;
        public CCollection MenuItemCollection
        {
            get
            {
                if (this._MenuItemCollection == null)
                {
                    if (this.CategoriesId == -1)
                        return new CCollection();
                    using (AccDBNAMenuItem db = new AccDBNAMenuItem())
                    {
                        CCollection MyCollection = new CCollection();
                        db.GetCollection(this, MyCollection);
                        this._MenuItemCollection = MyCollection;
                    }
                }
                return this._MenuItemCollection;
            }
        }
        #endregion
    }

    #region Class AccDB
    public class AccDBNACategories : AccDB
    {
        /// <summary>
        /// 
        /// </summary>
        public AccDBNACategories()
        {
        }
        /// <summary>
        ///Phuong thuc dung dien du lieu len Combo 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="cmb"></param>
        /// <param name="extName"></param>
        public override void FillComboBox(CLogin Login, ComboBox cmb, string extName)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT CategoriesId, CategoriesName";
                sql += " FROM  tblNACategories";
                sql += " WHERE (CategoriesId >= 0)";
                sql += " ORDER BY CategoriesName";
                DataTable table = p.OpenTable(sql, "tblNACategories");
                if (table != null)
                {
                    if (extName != "")
                    {
                        System.Data.DataRow row = table.NewRow();
                        row["CategoriesName"] = extName;
                        row["CategoriesId"] = "-1";
                        table.Rows.Add(row);
                    }
                    cmb.DataSource = table;
                    cmb.DisplayMember = "CategoriesName";
                    cmb.ValueMember = "CategoriesId";
                    p.Close();
                }
            }
        }
        /// <summary>
        ///Doc doi tuong tu CSDL theo ID chi dinh
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ObjId"></param>
        /// <returns></returns>
        public override Class GetClass(CLogin Login, IProvider p, object ObjId)
        {
            try
            {
                string sql = "SELECT * FROM tblNACategories WHERE (CategoriesId = " + ObjId + ")";
                DataTable table = p.OpenTable(sql, "tblNACategories");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    return new CNACategories(Login, p, table.Rows[0]);
                }
            }
            catch
            {
            }
            return null;
        }
        /// <summary>
        /// Luu doi tuong vao CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Save(IProvider p, Class obj)
        {
            CNACategories CategoriesVideos = (CNACategories)obj;
            string sql = "SELECT * FROM tblNACategories";
            sql += " WHERE CategoriesId =" + CategoriesVideos.CategoriesId + "";
            //sql +=" AND CompanyId =" + CategoriesVideos.CompanyId + "";
            DataTable dt = p.OpenTable(sql, "tblNACategories");
            if (dt != null)
            {
                DataRow row = null;
                if (dt.Rows.Count == 1 && CategoriesVideos.CategoriesId != -1)
                {
                    row = dt.Rows[0];
                }
                else
                {
                    CategoriesVideos.CategoriesId = Convert.ToInt32(p.InitIDKey("CategoriesId", "tblNACategories", null));
                    row = dt.NewRow();
                    dt.Rows.Add(row);
                }
                row["CategoriesId"] = CategoriesVideos.CategoriesId;
                row["CategoriesName"] = CategoriesVideos.CategoriesName;
                if (!p.UpdateTable("tblNACategories", dt))
                {
                    return false;
                }
                return true;
            }
            return false;
        }
        /// <summary>
        ///Xoa doi tuong khoi CSDL 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Delete(IProvider p, Class obj)
        {
            CNACategories CategoriesVideos = (CNACategories)obj;
            string sql = "DELETE FROM tblNACategories";
            sql += " WHERE CategoriesId =" + CategoriesVideos.CategoriesId + "";
            //sql +=" AND CompanyId =" + CategoriesVideos.CompanyId + "";
            return p.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(CLogin Login, IProvider p, CCollection Collection)
        {
            string sql = "SELECT * FROM tblNACategories ORDER BY CategoriesName";
            DataTable table = p.OpenTable(sql, "tblNACategories");
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    CNACategories o = new CNACategories(Login, p, row);
                    Collection.Add(o);
                }
            }
        }

        /// <summary>
        /// Lay ve danh sach cac doi tuong con cua doi obj neu co (danh cho loai du lieu quan he 2 bang cha con)
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(IProvider p, Class obj, CCollection Collection)
        {
        }
    }
    #endregion
}
